﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mitasoft.Component.Models;
using mitasoft.Component.Dapper;
using System.Data.SqlClient;
using System.Configuration;

namespace mitasoft.Component.Services
{
    public class BaseService : IDisposable
    {
        private readonly string ConnectionString;

        public BaseService()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
        }

        private SqlConnection _connection;
        public SqlConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    _connection = new SqlConnection(ConnectionString);
                    _connection.Open();
                }

                return _connection;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool dispose)
        {
            if (dispose)
            {
                if (_connection != null)
                {
                    _connection.Dispose();
                    _connection = null;
                }
            }
        }
    }
}
